package com.inquire.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.Collector;
import com.entity.Measuringpoint;
import com.entity.Org;
import com.entity.Terminal;
import com.file.entity.FileTerminal;
import com.inquire.entity.Children;
import com.inquire.entity.InquireCollector;
import com.inquire.entity.InquireOrgCollector;
import com.inquire.entity.InquireOrgMeter;
import com.inquire.entity.InquireOrgTerminal;
import com.inquire.entity.InquireTerminal;
import com.interfaces.CollectorDao;
import com.interfaces.MeasuringpointDao;
import com.interfaces.MeterDao;
import com.interfaces.OrgDao;
import com.interfaces.SysdicDao;
import com.interfaces.TerminalDao;
import com.setting.service.SysdicService;
import com.utils.JsonResult;

/**
 * @Title: FileInquireService.java
 * @Package com.inquire.service
 * @author 孙梦楠
 * @date 2017年8月7日 下午3:38:16
 * @version V1.0.0
 */

@Service
public class FileInquireService {

	private static final Logger log = LoggerFactory.getLogger(FileInquireService.class);

	@Autowired
	private MeasuringpointDao measuringpointdao;

	@Autowired
	private TerminalDao terminaldao;

	@Autowired
	private CollectorDao collectordao;

	@Autowired
	private MeterDao meterdao;

	@Autowired
	private SysdicDao sysdicdao;
	
	@Autowired
	private SysdicService sysdicservice;
	
	@Autowired
	private OrgDao orgdao;


	// 查找单个集中器
	@Transactional
	public JsonResult<InquireTerminal> listinquireterminal(Integer terminalid,String languagecookie) {
		if (terminalid != null) {

			Terminal terminal = terminaldao.findOne(terminalid);
			// 查采集器个数
			Integer collectorcount = measuringpointdao.countCollectorByTerminal(terminalid);
			// 查无采集器的单表个数
			Integer metercount = measuringpointdao.countMeterByTerminal(terminalid);

			InquireTerminal inquireterminal = new InquireTerminal();

			inquireterminal.setTerminalid(terminal.getTerminalid());
			inquireterminal.setTerminalcode(terminal.getTerminalcode());
			inquireterminal.setTerminalassetcode(terminal.getTerminalassetcode());
			inquireterminal.setTerminaltype(terminal.getTerminaltype());
			inquireterminal.setTerminaltypename(sysdicservice.finddisplay("0003", terminal.getTerminaltype(),languagecookie));
			inquireterminal.setTerminaladdress(terminal.getTerminaladdress());
			inquireterminal.setTerminalcommuncate(terminal.getTerminalcommuncate());
			inquireterminal.setTerminalcommuncatename(sysdicservice.finddisplay("0007", terminal.getTerminalcommuncate(),languagecookie));
			inquireterminal.setTerminalmade(terminal.getTerminalmade());
			inquireterminal.setTerminalmadename(sysdicservice.finddisplay("0008", terminal.getTerminalmade(),languagecookie));
			inquireterminal.setTerminalorg(terminal.getTerminalorg());
			inquireterminal.setTerminalorgname(sysdicservice.finddisplay("0001", terminal.getTerminalorg().toString(),languagecookie));
			inquireterminal.setTerminalstatus(terminal.getTerminalstatus());
			inquireterminal.setTerminalstatusname(sysdicservice.finddisplay("0009", terminal.getTerminalstatus(),languagecookie));
			inquireterminal.setCollectorcount(collectorcount.toString());
			inquireterminal.setMetercount(metercount.toString());

			return new JsonResult<InquireTerminal>(311, "查找单个集中器信息", inquireterminal);
		} else {

			return new JsonResult<InquireTerminal>(313, "查找单个集中器信息失败..ID为空", null);
		}
	}

	// 查找单个采集器
	@Transactional
	public JsonResult<InquireCollector> listinquirecollector(Integer collectorid,String languagecookie) {

		if (collectorid != null) {

			Collector collector = collectordao.findOne(collectorid);
			Integer metercount = measuringpointdao.countMeterByCollector(collectorid);

			InquireCollector inquirecollector = new InquireCollector();

			inquirecollector.setCollectorid(collector.getCollectorid());
			inquirecollector.setCollectorcode(collector.getCollectorcode());
			inquirecollector.setCollectorassetcode(collector.getCollectorassetcode());
			inquirecollector.setCollectortype(collector.getCollectortype());
			inquirecollector.setCollectortypename(sysdicservice.finddisplay("0004", collector.getCollectortype(),languagecookie));
			inquirecollector.setCollectorprotocol(collector.getCollectorprotocol());
			inquirecollector.setCollectorprotocolname(sysdicservice.finddisplay("0010", collector.getCollectorprotocol(),languagecookie));
			inquirecollector.setCollectormade(collector.getCollectormade());
			inquirecollector.setCollectormadename(sysdicservice.finddisplay("0008", collector.getCollectormade(),languagecookie));
			inquirecollector.setCollectororg(collector.getCollectororg());
			inquirecollector.setCollectororgname(sysdicservice.finddisplay("0001", collector.getCollectororg().toString(),languagecookie));
			inquirecollector.setCollectorstatus(collector.getCollectorstatus());
			inquirecollector.setCollectorstatusname(sysdicservice.finddisplay("0009", collector.getCollectorstatus(),languagecookie));
			inquirecollector.setMetercount(metercount);

			return new JsonResult<InquireCollector>(312, "查找单个采集器信息", inquirecollector);
		} else {

			return new JsonResult<InquireCollector>(314, "查找单个采集器信息失败..ID为空", null);
		}

	}


	
	
	// 列出设备架构
		@Transactional
		public JsonResult<Children> listinquireorg(String opeorgcookie,String opetypecookie) {
			
			Children inquireorg =new Children();
			inquireorg.setName("设备拓扑图");
			inquireorg.setType("0");
			List<Terminal> terminals =new ArrayList<Terminal>();
			if (opeorgcookie.equals("1")) {
				terminals = terminaldao.findByTypeAndTerminalstatusNot(opetypecookie, "4");
				
			} else if (orgdao.findByOrgvalue(opeorgcookie).getOrgfather().equals("0")) {
				List<Org> orgs = orgdao.findByOrgfather(opeorgcookie);
				for (Org org : orgs) {
					List<Terminal> terminals1 = terminaldao.findByTerminalorgAndTypeAndTerminalstatusNot(
							Integer.parseInt(org.getOrgvalue()), opetypecookie, "4");
					for (Terminal terminal : terminals1) {
						terminals.add(terminal);
					}
				}
			} else {
			terminals = terminaldao.findByTerminalorgAndTypeAndTerminalstatusNot(
						Integer.valueOf(opeorgcookie), opetypecookie, "4");
			}
			
			List<Children> inquireorgterminals = new ArrayList<Children>();
			for (int i = 0; i < terminals.size(); i++) {
				
				log.info("集中器序号  " + i);
				
				Children inquireorgterminal = new Children();
				// 集中器普通属性
				inquireorgterminal.setEquipid(terminals.get(i).getTerminalid());
				inquireorgterminal.setName("采集终端 - "+terminals.get(i).getTerminalcode());
				inquireorgterminal.setType("1");
				// 集中器下的采集器
				List<Measuringpoint> measuringpoints1 = measuringpointdao
						.listCollectorByTerminal100(terminals.get(i).getTerminalid());
				
				List<Children> inquireorgcollectors = new ArrayList<Children>();
				
				

				for (int j = 0; j < measuringpoints1.size(); j++) {

					log.info("采集器序号  " + j);
					Children inquireorgcollector = new Children();
					//采集器普通属性
					inquireorgcollector.setEquipid(measuringpoints1.get(j).getCollectorid());
					
					inquireorgcollector.setName("采集器 - "+
							collectordao.findOne(
									measuringpoints1.get(j).getCollectorid()).getCollectorcode());
					inquireorgcollector.setType("2");
					inquireorgcollectors.add(inquireorgcollector);

					// 采集器下的单表
					List<Measuringpoint> measuringpoints2 = measuringpointdao
							.listMeterByCollector100(measuringpoints1.get(j).getCollectorid());
					List<Children> inquireorgmeters1 = new ArrayList<Children>();
					for (int l = 0; l < measuringpoints2.size(); l++) {
						log.info("连接采集器的表   " + l);
						Children inquireorgmeter = new Children();
						Integer meterid = measuringpoints2.get(l).getMeterid();
						//单表属性
						inquireorgmeter.setEquipid(meterid);
						inquireorgmeter.setName("水表 - "+meterdao.findOne(meterid).getMetercode());
						inquireorgmeter.setType("3");
						inquireorgmeters1.add(inquireorgmeter);

					}
					inquireorgcollector.setChildren(inquireorgmeters1);

				}
				
				// 集中器下的单表
				List<Measuringpoint> measuringpoints3 = measuringpointdao
						.listMeterByTerminal100(terminals.get(i).getTerminalid());
				
				
				
				for (int k = 0; k < measuringpoints3.size(); k++) {
					log.info("单表序号  " + k);
					Children inquireorgmeter = new Children();
					Integer meterid = measuringpoints3.get(k).getMeterid();
					//单表属性
					inquireorgmeter.setEquipid(meterid);
					inquireorgmeter.setName("水表 - "+meterdao.findOne(meterid).getMetercode());
					inquireorgmeter.setType("3");
					inquireorgcollectors.add(inquireorgmeter);
				}
				inquireorgterminal.setChildren(inquireorgcollectors);

				inquireorgterminals.add(inquireorgterminal);
			}
			
			inquireorg.setChildren(inquireorgterminals);
			return new JsonResult<Children>(315, "列出设备架构", inquireorg);

		}
	



}
